%%%-------------------------------------------------------------------
%%% @author mirahs(2892727493@qq.com)
%%% @doc
%%%     开发工具
%%% @end
%%%-------------------------------------------------------------------
-module(dev).

-export([
    %热更
    up/0
    ,up/1
    ,up/2
]).

-include("common.hrl").


%%%===================================================================
%%% API functions
%%%===================================================================

%% 热更新所有模块
up() ->
    up_do([]).

%% 热更新所有模块(强制更新)
up(force) ->
    up_do([force]);

%% 热更新指定模块
up(ModList) when is_list(ModList) ->
    up_do([ModList]).

%% 热更新指定模块(强制更新)
up(ModList, force) when is_list(ModList) ->
    up_do([ModList, force]).


%%%===================================================================
%%% Internal functions
%%%===================================================================

up_do(Args) ->
    ?ECHO("--- begin hot swapping node: ~p", [node()]),
    Rtn = rpc:call(node(), sys_code, up, Args, infinity),
    up_print(Rtn).

up_print([]) -> ok;
up_print([{M, R} | T]) ->
    case R of
        ok ->
            ?ECHO("# load module success: ~p", [M]);
        {error, Reason} ->
            ?ECHO("* load module failed [~p]: ~p", [M, Reason])
    end,
    up_print(T).
